home *** CD-ROM | disk | FTP | other *** search
/ PC User 2002 April / Disc 2 / PCUSER0402D2.iso / software / utils / files / wincron.exe / data1.cab / Sample_Scripts / C / parse_filename.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-20  |  4.6 KB  |  147 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "win32.h"
  5.  
  6. #define _MAX_PATH   260 /* max. length of full pathname */
  7. #define _MAX_DRIVE  3   /* max. length of drive component */
  8. #define _MAX_DIR    256 /* max. length of path component */
  9. #define _MAX_FNAME  256 /* max. length of file name component */
  10. #define _MAX_EXT    256 /* max. length of extension component */
  11. #define __min(a,b)  (((a) < (b)) ? (a) : (b))
  12.  
  13. void parse (char *path, char *drive, char *dir, char *fname, char *ext);
  14.  
  15. int main(int argc, char **argv)
  16. {
  17.  
  18.     /*char path[_MAX_PATH];*/
  19.     char drive[_MAX_DRIVE];
  20.     char dir[_MAX_DIR];
  21.     char fname[_MAX_FNAME];
  22.     char ext[_MAX_EXT];
  23.  
  24.     if (argc <= 1)
  25.     {
  26.         printf("parse_filename usage:\n");
  27.         printf("parse_filename path_name_to_parse\n");
  28.         printf("The output will be placed in the environment variables:\n");
  29.         printf("DRIVE, DIR, FNAME, and EXT\n");
  30.         return 0;
  31.     }
  32.     
  33.     /* now parse the FULL_PATH into components */
  34.     parse (argv[1], drive, dir, fname, ext);
  35.  
  36.     /* set each component in the environment */
  37.     SetEnvironmentVariable("drive", drive);
  38.     SetEnvironmentVariable("dir", dir);
  39.     SetEnvironmentVariable("fname", fname);
  40.     SetEnvironmentVariable("ext", ext);
  41.  
  42.     return 0;
  43. }
  44.  
  45.  
  46. void parse (char *path, char *drive, char *dir, char *fname, char *ext)
  47. {
  48.         char *p;
  49.         char *last_slash = NULL, *dot = NULL;
  50.         unsigned len;
  51.  
  52.         /* we assume that the path argument has the following form:
  53.          *  <drive><dir><fname><ext>
  54.          */
  55.         
  56.         /* extract drive letter and :, if any */
  57.  
  58.         if ((strlen(path) >= (_MAX_DRIVE - 2)) && (*(path + _MAX_DRIVE - 2) == ':')) {
  59.             if (drive) {
  60.                 strncpy(drive, path, _MAX_DRIVE - 1);
  61.                 *(drive + _MAX_DRIVE-1) = '\0';
  62.             }
  63.             path += _MAX_DRIVE - 1;
  64.         }
  65.         else if (drive) {
  66.             *drive = '\0';
  67.         }
  68.  
  69.         /* extract path string, if any.  Path now points to the first character
  70.          * of the path, if any, or the filename or extension, if no path was
  71.          * specified.  Scan ahead for the last occurence, if any, of a '/' or
  72.          * '\' path separator character.  If none is found, there is no path.
  73.          * We will also note the last '.' character found, if any, to aid in
  74.          * handling the extension.
  75.          */
  76.  
  77.         for (last_slash = NULL, p = (char *)path; *p; p++) {
  78.             if (*p == '/' || *p == '\\')
  79.                 /* point to one beyond for later copy */
  80.                 last_slash = p + 1;
  81.             else if (*p == '.')
  82.                 dot = p;
  83.         }
  84.  
  85.         if (last_slash) {
  86.  
  87.             /* found a path - copy up through last_slash or max. characters
  88.              * allowed, whichever is smaller
  89.              */
  90.  
  91.             if (dir) {
  92.                 len = __min(((char *)last_slash - (char *)path) / sizeof(char),
  93.                     (_MAX_DIR - 1));
  94.                 strncpy(dir, path, len);
  95.                 *(dir + len) = '\0';
  96.             }
  97.             path = last_slash;
  98.         }
  99.         else if (dir) {
  100.  
  101.             /* no path found */
  102.  
  103.             *dir = '\0';
  104.         }
  105.  
  106.         /* extract file name and extension, if any.  Path now points to the
  107.          * first character of the file name, if any, or the extension if no
  108.          * file name was given.  Dot points to the '.' beginning the extension,
  109.          * if any.
  110.          */
  111.  
  112.         if (dot && (dot >= path)) {
  113.             /* found the marker for an extension - copy the file name up to
  114.              * the '.'.
  115.              */
  116.             if (fname) {
  117.                 len = __min(((char *)dot - (char *)path) / sizeof(char),
  118.                     (_MAX_FNAME - 1));
  119.                 strncpy(fname, path, len);
  120.                 *(fname + len) = '\0';
  121.             }
  122.             /* now we can get the extension - remember that p still points
  123.              * to the terminating nul character of path.
  124.              */
  125.             if (ext) {
  126.                 len = __min(((char *)p - (char *)dot) / sizeof(char),
  127.                     (_MAX_EXT - 1));
  128.                 strncpy(ext, dot, len);
  129.                 *(ext + len) = '\0';
  130.             }
  131.         }
  132.         else {
  133.             /* found no extension, give empty extension and copy rest of
  134.              * string into fname.
  135.              */
  136.             if (fname) {
  137.                 len = __min(((char *)p - (char *)path) / sizeof(char),
  138.                     (_MAX_FNAME - 1));
  139.                 strncpy(fname, path, len);
  140.                 *(fname + len) = '\0';
  141.             }
  142.             if (ext) {
  143.                 *ext = '\0';
  144.             }
  145.         }
  146. }
  147.